﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Microsoft.Practices.Unity;
using System.ServiceModel.Activation;

namespace S3System.ServiceImplementation
{
   [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class S3SystemQueryService :ServiceContract.IS3SystemQueryService
    {

        [Dependency]
        public BusinessLogic.CRM.IBCCustomerQueries BCCustomerQueries { get; set; }

        #region IS3SystemQueryService Members

        public ServiceContract.MessageContracts.SearchCustomerResponse SearchCustomers(ServiceContract.MessageContracts.SearchCustomerRequest request)
        {
            ServiceContract.MessageContracts.SearchCustomerResponse response = new ServiceContract.MessageContracts.SearchCustomerResponse(); 
            try
            {
                var result = BCCustomerQueries.GetAll();
                var dtos = new List<ServiceContract.DataContracts.DTOCustomer>();
                foreach (var item in result)
                {
                    var dto = new ServiceContract.DataContracts.DTOCustomer();
                    dto.Id = item.Id;
                    dto.FirstName = item.FirstName;
                    dtos.Add(dto);
                }
                response.TotalCustomers = dtos.Count;
                response.Customers = dtos;
            }
            catch (Exception ex)
            {
                var fault =  new ServiceContract.FaultContracts.S3SystemErrorInfo(); 
                fault.SupportMessage = ex.Message; 
                fault.UIMessage = "lo sentimos....";
                throw new FaultException<ServiceContract.FaultContracts.S3SystemErrorInfo>(fault, "error service...");                                
            }
            return response;
        }

        #endregion
    }
}
